import 'dart:async';
import 'dart:convert';

import 'package:eplorex/providers/user_context.dart';
import 'package:eplorex/utils/HttpUtil.dart';
import 'package:flutter/services.dart';

import '../models/HomeItemModel.dart';
import '../models/moment/banner_model.dart';

/// 首页接口
class HomeDao {
  static Future<List<HomeItemModel>> fetchMy() async {
    final response = await HttpUtil.getInstance()
        .get("/api/moment/v1/my/feeds", data: {'uid': UserContext.cxt()!.uid});
    if (response?.statusCode == 200) {
      return response!.data['data']
          .map<HomeItemModel>((ele) => HomeItemModel.fromJson(ele))
          .toList();
    } else {
      throw Exception('Failed to load home_page.json');
    }
  }

  static Future<List<HomeItemModel>> fetch() async {
    final response = await HttpUtil.getInstance()
        .get("/api/moment/v1/feeds", data: {'uid': UserContext.cxt()!.uid});
    if (response?.statusCode == 200) {
      return response!.data['data']
          .map<HomeItemModel>((ele) => HomeItemModel.fromJson(ele))
          .toList();
    }
    return [];
  }

  static Future<List<BannerModel>> fetchBanner() async {
    var response = await HttpUtil.getInstance()
        .get("/api/home/feed/v1/banner", data: {'uid': UserContext.cxt()!.uid});
    if (response?.statusCode == 200) {
      return response!.data['data']
          .map<BannerModel>((ele) => BannerModel.fromJson(ele))
          .toList();
    }
    return [];
  }
}
